# url.py
import urllib
from urllib import request
from urllib.parse import urlparse

try:
    encoded_url = request.quote("https://www.baidu.com")
    decoded_url = request.unquote(encoded_url)
    response = request.urlopen(decoded_url)
    print(response.getcode() == response.status == response.code)
    for k, v in response.getheaders():
        print("{}:{}".format(k, v))
    print(response.read())
except urllib.error.HTTPError as e:
    if str(e.code).startswith("4"):
        print(e)
else:
    print("server error")

addr = "http://localhost:8080/api?name=John&age=19"
result = urlparse(addr)
print(result.scheme)
print(result.hostname)
print(result.port)
print(result.path)
print(result.query)
